package org.lgiavedoni.android.franq.util.phone;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.lgiavedoni.android.franq.service.phone.ContextService;

import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class SpeechUtil implements OnInitListener {

	public final static SpeechUtil inst = new SpeechUtil();

	private static TextToSpeech tts;

	private SpeechUtil() {
		if (ContextService.inst.isApplicationContextLoaded()) {
			Intent checkIntent = new Intent();
			checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
			tts = new TextToSpeech(ContextService.inst.getApplicationContext(), this);
			tts.setLanguage(Locale.US);
		}
	}

	@Override
	public void onInit(int arg0) {
	}

	public void speech(String text) {
		if (tts != null) {
			text = StringUtils.remove(text, '[');
			text = StringUtils.remove(text, ']');
			// Ignore exceptions
			if (text.indexOf("Exception") > 0) {
				return;
			}
			// tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
			tts.speak(text, TextToSpeech.QUEUE_ADD, null);
		}
	}

}
